﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp.Controls
{
    public partial class BrandProduct_PopProduct : System.Web.UI.UserControl
    {
        private ProductManager m_ProductManager = new ProductManager();

        private int m_ShowCount = 3;
        public int ShowCount
        {
            get { return m_ShowCount; }
            set { m_ShowCount = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindProduct();
            }
        }

        private void BindProduct()
        {
            List<Cosmetic.DataEntity.ProductInfo> productList = new List<Cosmetic.DataEntity.ProductInfo>();
            productList = this.m_ProductManager.GetProductInfoByFirstCategorys(2);
            int theLenght = productList.Count;
            Random ran = new Random();
            int[] theNum = new int[m_ShowCount];
            int i = 0;
            //while (true)
            //{
            //    int tem = ran.Next(theLenght);
            //    if (!theNum.Contains(tem) && i < m_ShowCount)
            //    {
            //        theNum[i] = tem;
            //        i++;
            //    }
            //    else if (i == m_ShowCount)
            //    {
            //        break;
            //    }
            //}
            List<ProductInfo> showList = new List<ProductInfo>();
            if (theLenght < m_ShowCount)
            {
                for (int j = 0; j < theLenght; j++)
                {
                    showList.Add(productList[theNum[j]]);
                }
            }
            else
            {

                int tem = ran.Next(theLenght);
                for (int j = tem; j < theLenght; j++)
                {
                    if (i < m_ShowCount)
                    {
                        theNum[i] = j;
                        i++;
                    }
                    else
                    {
                        break;
                    }
                }
                //说明还没有将数组填充完。
                int lastCount = m_ShowCount - i - 1;
                i++;
                if (i < m_ShowCount)
                {
                    for (int a = 0; a < lastCount; a++)
                    {
                        theNum[i] = a;
                        i++;
                    }
                }

                for (int j = 0; j < m_ShowCount; j++)
                {
                    showList.Add(productList[theNum[j]]);
                }
            }
            this.lvProduct.DataSource = showList; //this.m_ProductManager.GetProductInfoByBrand(m_Product).OrderByDescending(theProduct => theProduct.Views).Take(m_ShowCount).ToList();
            this.lvProduct.DataBind();
        }
    }
}